import fred
import sys,os
import q
sys.path.append("/home/vvishwa/python")
conn = q.conn('localhost', 5000)

fred.key('429d7dba943722523e1329dfaefd3b2a')
swapcurve = ['DSWP1', 'DSWP2', 'DSWP3', 'DSWP4', 'DSWP5', 'DSWP7', 'DSWP10', 'DSWP30']
for curve in swapcurve:
    curveTable = '%s:([]date:`datetime$() ;rate:`float$())' % curve
    conn(curveTable)
    fred_data = fred.observations(curve)
    data = fred_data.values()
    nv = data[8]
    for da in nv:
        try:
            newrecord = '(%sT;%f)' % (str(da['date']).replace('-','.'), float(da['value']))
            insertcommand = '`%s insert %s' % (curve, newrecord)
            conn(insertcommand)
        except ValueError:
            print 'Skipping'
    saveCommand = '`:%s set %s' %(curve, curve)
    conn(saveCommand)